package com.sojson.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.constant.ConstantDict;
import com.sojson.controller.po.param.verify.DictDataParamVerify;
import com.sojson.entity.po.DictData;
import com.sojson.result.Result;
import com.sojson.service.DictDataService;
import com.sojson.util.StringUtil;

import io.swagger.annotations.Api;

/**
 * 字典类型描述表控制器
 * 
 * @author liu
 * @date 2021-04-13
 */
@Api(tags = "字典类型描述表管理")
@RestController
@RequestMapping(value = "/dictData")
public class DictDataController {

    @Resource
    private DictDataService dictDataServiceImpl;

    /**
     * 单条插入
     * 
     * @param dictData
     * @return
     */
    @PostMapping(value = "/insert")
    @PreAuthorize("@ss.hasPermi('/dictData/insert')")
    public Object insert(@RequestBody DictData dictData) {
        // 校验参数
        Result<?> paramResult = DictDataParamVerify.insert(dictData);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictData.insertInit();
        dictDataServiceImpl.insert(dictData);
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dictData
     * @return
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/dictData/updatePartialById')")
    public Object updatePartialById(@RequestBody DictData dictData) {
        // 校验参数
        Result<?> paramResult = DictDataParamVerify.updatePartialById(dictData);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictData.updateInit();
        dictData.setUpdateNull();
        dictDataServiceImpl.updatePartialById(dictData);
        return Result.success();
    }

    /**
     * 通过code列表删除(物理删除)
     * 
     * @param codes
     * @return
     */
    @DeleteMapping(value = "/deleteByCodeList")
    @PreAuthorize("@ss.hasPermi('/dictData/deleteByCodeList')")
    public Object deleteByCodeList(@RequestParam("codes") List<String> codes) {
        if (codes.size() == 0) {
            return Result.success();
        }

        return Result.success(dictDataServiceImpl.deleteByCodeList(codes));
    }

    /**
     * 单条插入
     * 
     * @param dictData
     * @return
     */
    @DeleteMapping(value = "/deleteByIdList/{ids}")
    @PreAuthorize("@ss.hasPermi('/dictData/deleteByIdList')")
    public Object deleteByIdList(@PathVariable("ids") ArrayList<Integer> ids) {
        if (ids.size() < 1) {
            return Result.success();
        }

        dictDataServiceImpl.deleteByIdList(ids);
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param dictData
     * @return
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/dictData/findByDto')")
    public Object findByDto(DictData dictData) {
        List<DictData> findByDto = dictDataServiceImpl.findByDto(dictData);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping(value = "/findById")
    public Object findById(Integer id) {
        DictData findById = dictDataServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过code查询
     * 
     * @param code
     * @return
     */
    @GetMapping(value = "/findByCode")
    public Object findByCode(DictData dictData) {
        if (StringUtil.isBlank(dictData.getCode())) {
            throw new TransException("code不能为空！");
        }
        List<DictData> findByCode = dictDataServiceImpl.findByCode(dictData);
        return Result.success(findByCode, findByCode);
    }

    /**
     * 单条插入(职位详细类型)
     * 
     * @param dictData
     * @return
     */
    @PostMapping(value = "/insertPostDescType")
    @PreAuthorize("@ss.hasPermi('/dictData/insertPostDescType')")
    public Object insertPostDescType(@RequestBody DictData dictData) {
        dictData.setCode(ConstantDict.POST_DESC_TYPE);
        // 校验参数
        Result<?> paramResult = DictDataParamVerify.insertType(dictData);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictData.insertInit();
        dictData.setValue(System.currentTimeMillis() + "");
        dictDataServiceImpl.insert(dictData);
        return Result.success();
    }

    /**
     * 通过code查询(职位详细类型)
     * 
     * @param code
     * @return
     */
    @GetMapping(value = "/findPostDescType")
    public Object findPostDescType(DictData dictData) {
        dictData.setCode(ConstantDict.POST_DESC_TYPE);
        List<DictData> findByCode = dictDataServiceImpl.findByCode(dictData);
        return Result.success(findByCode, findByCode);
    }

    /**
     * 单条插入(商户类型)
     * 
     * @param dictData
     * @return
     */
    @PostMapping(value = "/insertMerchantType")
    @PreAuthorize("@ss.hasPermi('/dictData/insertMerchantType')")
    public Object insertMerchantType(@RequestBody DictData dictData) {
        dictData.setCode(ConstantDict.MERCHANT_TYPE);
        // 校验参数
        Result<?> paramResult = DictDataParamVerify.insertType(dictData);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictData.insertInit();
        dictData.setValue(System.currentTimeMillis() + "");
        dictDataServiceImpl.insert(dictData);
        return Result.success();
    }

    /**
     * 通过code查询(商户类型)
     * 
     * @param code
     * @return
     */
    @GetMapping(value = "/findMerchantType")
    public Object findMerchantType(DictData dictData) {
        dictData.setCode(ConstantDict.MERCHANT_TYPE);
        List<DictData> findByCode = dictDataServiceImpl.findByCode(dictData);
        return Result.success(findByCode, findByCode);
    }

    /**
     * 单条插入(酒店房间类型)
     * 
     * @param dictData
     * @return
     */
    @PostMapping(value = "/insertHotelRoomType")
    @PreAuthorize("@ss.hasPermi('/dictData/insert')")
    public Object insertHotelRoomType(@RequestBody DictData dictData) {
        dictData.setCode(ConstantDict.HOTEL_TYPE);
        // 校验参数
        Result<?> paramResult = DictDataParamVerify.insertType(dictData);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictData.insertInit();
        dictData.setValue(System.currentTimeMillis() + "");
        dictDataServiceImpl.insert(dictData);
        return Result.success();
    }

    /**
     * 通过code查询(酒店房间类型)
     * 
     * @param code
     * @return
     */
    @GetMapping(value = "/findHotelRoomType")
    public Object findHotelRoomType(DictData dictData) {
        dictData.setCode(ConstantDict.HOTEL_TYPE);
        List<DictData> findByCode = dictDataServiceImpl.findByCode(dictData);
        return Result.success(findByCode, findByCode);
    }

}